package queue;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue array = new ArrayQueue(3);

        System.out.println(array.isEmpty());
        System.out.println(array.isFull());
//        array.getQueue();
        array.addQueue(1);
        array.addQueue(2);
        array.showQueue();
        System.out.println(array.getQueue());
        System.out.println(array.headQueue());
        array.addQueue(3);

    }
}

//使用数组模拟队列
class ArrayQueue {
    private int maxSize;
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//存放数据

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;//指向队列头部，分析出front是指向队列头的前一个位置
        rear = -1;//指向队列尾，指向队列尾的数据，（即：就是队列的最后一个数据）
    }

    //判断队列是否为满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列满了");
        }
        rear++;
        arr[rear] = n;
        return;
    }

    //去数据
    public int getQueue() {
        if (isEmpty()) {
            //通过抛出异常
            throw new RuntimeException("队列为空");
        }
        front++;
        return arr[front];
    }

    //显示queue所有数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("数组为空");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(i + "\t" + arr[i] + "\t");
        }
    }

    //显示对头数据，不取出来
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];
    }
}
